<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 2018/6/23
 * Time: 17:54
 */

namespace app\common\model;


use think\Model;

class Notes extends Model
{
    public function getNotesById($id,$page,$pageSize = 10){
        if(!$id && !is_numeric($id)){
            exception("传递的ID不合法");
        }

        $where = array(
            'n.status'=>1,
            'n.user_id' => $id
        );

        $offset = ($page - 1) * $pageSize;

        return $this->where($where)->limit($offset,$pageSize)->alias("n")->join("xm_courses c","n.course_id=c.id")->field("n.content,n.create_time,c.id as CourseId,n.id")->select();
    }

    public function getNotesCount($id){
        if(!$id && !is_numeric($id)){
            exception("传递的ID不合法");
        }

        $where = array(
            'status' => 1,
            'course_id' => $id
        );

        return $this->where($where)->count();
    }

    public function getNoteByNoteId($id){
        if(!$id && !is_numeric($id)){
            exception("传递的ID不合法");
        }

        $where = array(
            'n.id' => $id,
            'n.status' => 1,
        );

        return $this->where($where)->alias("n")->join("xm_courses c","n.course_id=c.id")->field("n.content,n.create_time,c.id as CourseId,n.id")->select();

    }

    //修改笔记
    public function EditNoteById($id,$data){
        if(!$id && !is_numeric($id)){
            exception("传递的ID不合法");
        }

        return $this->save($data,['id'=>$id]);
    }

    //添加笔记
    public function getNoteAdd($data){
        if(!$data && !is_array($data)){
            exception("传递的数据不合法");
        }
        $this->save($data);
        return $this->id;
    }
}